package com.zrrd.objectStream;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;

/**
 * @author WanJl
 * @version 1.0
 * @title ObjectInputSteamDemo01
 * @description 反序列化
 * @create 2024/5/26
 */
public class ObjectInputSteamDemo01 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        ObjectInputStream ois = null;

        try {
            fis=new FileInputStream("E:/users/序列化.txt");
            ois=new ObjectInputStream(fis);

            //反序列化，将文件中保存的对象和属性值反序列化为对象
            Object o = ois.readObject();
            if (o instanceof Student) {//判断对象o是否属于Student类型
                Student s = (Student)o;
                System.out.println(s.getName());
                System.out.println(s.getAge());
                System.out.println(s);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                ois.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
